package com.xy.mix.xiaoxi.configuration;

import com.xy.biz.uc.manager.UserManager;
import com.xy.tool.jwt.TokenParser;
import com.xy.tool.webmvc.interceptor.LogInterceptor;
import com.xy.tool.webmvc.interceptor.TeamManagementInterceptor;
import com.xy.tool.webmvc.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Created by zsp on 2018/7/13.
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Value("${spring.application.name}")
    private String appName;
    @Autowired
    private TokenParser tokenParser;
    @Autowired
    private UserManager userManager;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(false)
                //.exposedHeaders(HttpHeaders.SET_COOKIE)
                .maxAge(3600L);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TokenInterceptor(tokenParser))
                .excludePathPatterns("/swagger-resources/**",
                        "/null/swagger-resources/**",
                        "/webjars/**",
                        "/v2/**",
                        "/swagger-ui.html/**",
                        "/error")
                .excludePathPatterns("/api/auth/captcha/**")
                .excludePathPatterns("/api/auth/logon/**")
                .excludePathPatterns("/api/fd/pdf/get_document_by_pdf")
                .excludePathPatterns("/api/eim/document_coa/list_by_code")
                .excludePathPatterns("/api/ep/delivery_tracking/route_receive")
                .excludePathPatterns("/api/ep/delivery_tracking/order_status_receive")
                .excludePathPatterns("/api/eim/document_coa/open_pdf")
                .excludePathPatterns("/only/**");
        registry.addInterceptor(new TeamManagementInterceptor(userManager))
                .addPathPatterns("/api/uc/team/save",
                        "/api/uc/team/invite_join",
                        "/api/uc/team/update_team_user_role",
                        "/api/uc/team/update_team_user_role",
                        "/api/uc/team/deleted_team_user",
                        "/api/uc/team/update");
//        registry.addInterceptor(new TeamRepositoryManagementInterceptor(userManager))
//                .addPathPatterns("/api/fd/repository/save",
//                        "/api/fd/repository/update");
        registry.addInterceptor(new LogInterceptor(appName));
    }

}
